package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.ConsultManagement;
import com.ruoyi.recycle.domain.EssayQuestion;
import com.ruoyi.recycle.dto.EssayQuestionDTO;
import com.ruoyi.recycle.service.IEssayQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 问答库设置Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/essay")
@Api(tags = "问答库设置")
public class EssayQuestionController extends BaseController {
    @Autowired
    private IEssayQuestionService essayQuestionService;

    /**
     * 查询问答库设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EssayQuestion essayQuestion) {
        startPage();
        List<EssayQuestion> list = essayQuestionService.selectEssayQuestionList(essayQuestion);
        return getDataTable(list);
    }

    /**
     * 分页查询问答库设置列表
     */
    @PostMapping("/essayPageQuery")
    @ApiOperation("分页查询问答库设置列表")
    public IPage<EssayQuestion> essayPageQuery(@RequestBody EssayQuestionDTO queryDTO) throws Exception{
        final EssayQuestion factory = new EssayQuestion();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<EssayQuestion> wrapper = Wrappers.lambdaQuery(factory);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(EssayQuestion::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));

        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(EssayQuestion::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(EssayQuestion::getUpdatedTime);
        return essayQuestionService.page(new Page<EssayQuestion>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出问答库设置列表
     */
    @RequiresPermissions("recycle:essay:export")
    @Log(title = "问答库设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出问答库设置列表")
    public void export(HttpServletResponse response, EssayQuestion essayQuestion) {
        List<EssayQuestion> list = essayQuestionService.selectEssayQuestionList(essayQuestion);
        ExcelUtil<EssayQuestion> util = new ExcelUtil<EssayQuestion>(EssayQuestion.class);
        util.exportExcel(response, list, "问答库设置数据");
    }

    /**
     * 获取问答库设置详细信息
     */
    @RequiresPermissions("recycle:essay:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取问答库设置详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(essayQuestionService.selectEssayQuestionById(id));
    }

    /**
     * 新增问答库设置
     */
    @RequiresPermissions("recycle:essay:add")
    @Log(title = "问答库设置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增问答库设置")
    public AjaxResult add(@RequestBody EssayQuestion essayQuestion) {
        essayQuestion.setCreatedTime(new Date());
        essayQuestion.setUpdatedTime(new Date());
        return toAjax(essayQuestionService.insertEssayQuestion(essayQuestion));
    }

    /**
     * 修改问答库设置
     */
    @RequiresPermissions("recycle:essay:edit")
    @Log(title = "问答库设置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改问答库设置")
    public AjaxResult edit(@RequestBody EssayQuestion essayQuestion) {
        essayQuestion.setUpdatedTime(new Date());
        return toAjax(essayQuestionService.updateEssayQuestion(essayQuestion));
    }

    /**
     * 删除问答库设置
     */
    @RequiresPermissions("recycle:essay:remove")
    @Log(title = "问答库设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除问答库设置")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(essayQuestionService.deleteEssayQuestionByIds(ids));
    }
}
